package com.breakfast.fun.ui.user.info;

import android.util.Log;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.User;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.io.File;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yukai on 16/9/20.
 */
public class UserInfoPresenter extends UserInfoContract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    public void uploadPhoto(File file) {

        showLoadingDialog(true);

        rxManager.add(model.uploadPhotoModel(file)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<User>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(User user) {
                        hideLoadingDialog();
                        if (user.isSuccess()) {
                            view.showSuccess(user.getData());
                        } else {
                            T.show(user.getMsg());
                        }

                    }

                }));
    }

    @Override
    public void saveInfo(String name, String sex, String birthday) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("nickname", name);
        map.put("sex", sex);
        map.put("id", Constant.getUserData().getId() + "");
        map.put("action", "update");
        if (!birthday.equals("")) {
            map.put("birthday", birthday);
        }
        showLoadingDialog(true);
        rxManager.add(model.getSaveInfoModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<User>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(User user) {
                        hideLoadingDialog();
                        if (user.isSuccess()) {
                            view.showInfoSuccess(user.getData());
                        } else {
                            T.show(user.getMsg());
                        }
                    }

                }));
    }
}
